package com.article.controller.system;

import com.article.dto.ReturnError;
import com.article.enums.ReturnInfoEnum;
import org.apache.commons.fileupload.disk.DiskFileItem;
import org.csource.utils.FastDFSClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Created by Administrator on 2017/5/18.
 */
@RestController
@RequestMapping("/file")
public class UploadController {
    private final static Logger logger = LoggerFactory.getLogger(UploadController.class);

    @RequestMapping(value = "/upload")
    public ReturnError upload(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //创建一个通用的多部分解析器
        CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());
        //判断 request 是否有文件上传,即多部分请求
        List<String> fileNames = new ArrayList<String>();
        try {
            if (multipartResolver.isMultipart(request)) {
                //转换成多部分request
                MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
                //取得request中的所有文件名
                Iterator<String> iter = multiRequest.getFileNames();
                CommonsMultipartFile cf = null;
                DiskFileItem fi = null;
                File f = null;
                while (iter.hasNext()) {
                    //记录上传过程起始时的时间，用来计算上传时间
                    int pre = (int) System.currentTimeMillis();
                    //取得上传文件
                    MultipartFile file = multiRequest.getFile(iter.next());

                    if (file != null) {
                        //取得当前上传文件的文件名称
                        String myFileName = file.getOriginalFilename();
                        //如果名称不为“”,说明该文件存在，否则说明该文件不存在
                        if (!"".equals(myFileName.trim())) {
                            String fileId = FastDFSClient.uploadFile2(file, myFileName.trim());
                            fileNames.add(fileId);
                            //fileNames.add(myFileName.trim());
                            //System.out.println(myFileName);
                            //重命名上传后的文件名 ,
                            //String type = myFileName.substring(myFileName.lastIndexOf("."));
                            //String fileName = System.nanoTime() + type;
                            ////定义上传路径
                            //String path = PATH_FOLDER + "\\" + fileName;
                            //System.out.println(path);
                            //File localFile = new File(path);
                            //file.transferTo(localFile);
                        }
                    }
                    //记录上传该文件后的时间
                    int finaltime = (int) System.currentTimeMillis();
                    //System.out.println(finaltime - pre);
                }

            }
            return new ReturnError(false, ReturnInfoEnum.SUCCESS.getType(), fileNames);
        } catch (Exception e) {
            return new ReturnError(true, ReturnInfoEnum.ERROR.getType(), ReturnInfoEnum.ERROR.getInfo());
        }
    }
}
